/**
 ******************************************************************************
 * @file           : main.c
 * @brief          : I2C EEPROM Example
 ******************************************************************************
 * @attention
 *
 * I2C读写AT24CXX EEPROM示例
 * 演示EEPROM的写入、读取和数据验证
 *
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "stm32f1xx_hal.h"
#include "./BSP/IIC/bsp_iic.h"
#include "./Devices/24CXX/bsp_24cxx.h"
#include "./BSP/USART/bsp_usart.h"
#include <stdio.h>
#include <string.h>

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void EEPROM_Test(void);

/**
 * @brief  主函数
 */
int main(void)
{
    /* HAL库初始化 */
    HAL_Init();
    
    /* 系统时钟配置 */
    SystemClock_Config();
    
    /* 初始化串口（用于printf调试） */
    usart_init(115200);
    
    printf("\r\n========================================\r\n");
    printf("  AT24CXX EEPROM 测试程序\r\n");
    printf("========================================\r\n\r\n");
    
    /* 初始化I2C */
    iic_init();
    
    /* 初始化EEPROM */
    eeprom_24cxx_init();
    
    /* EEPROM测试 */
    EEPROM_Test();
    
    /* 主循环 */
    while (1)
    {
        HAL_Delay(1000);
    }
}

/**
 * @brief  EEPROM测试函数
 */
void EEPROM_Test(void)
{
    uint8_t write_buf[32];
    uint8_t read_buf[32];
    
    /* 准备写入数据 */
    const char *test_str = "Hello, AT24CXX!";
    strcpy((char *)write_buf, test_str);
    
    printf("【测试1】字符串写入测试\r\n");
    printf("写入数据: %s\r\n", write_buf);
    
    /* 写入EEPROM */
    if (eeprom_24cxx_write(0x0000, write_buf, strlen(test_str) + 1) == EEPROM_24CXX_OK)
    {
        printf("写入成功！\r\n");
    }
    else
    {
        printf("写入失败！\r\n");
        return;
    }
    
    /* 读取EEPROM */
    printf("\r\n【测试2】数据读取测试\r\n");
    if (eeprom_24cxx_read(0x0000, read_buf, strlen(test_str) + 1) == EEPROM_24CXX_OK)
    {
        printf("读取成功！\r\n");
        printf("读取数据: %s\r\n", read_buf);
    }
    else
    {
        printf("读取失败！\r\n");
        return;
    }
    
    /* 数据验证 */
    printf("\r\n【测试3】数据验证\r\n");
    if (strcmp((char *)write_buf, (char *)read_buf) == 0)
    {
        printf("✅ 数据校验通过！\r\n");
    }
    else
    {
        printf("❌ 数据校验失败！\r\n");
    }
    
    /* 多字节测试 */
    printf("\r\n【测试4】多字节写入测试\r\n");
    for (int i = 0; i < 32; i++)
    {
        write_buf[i] = i;
    }
    
    eeprom_24cxx_write(0x0100, write_buf, 32);
    eeprom_24cxx_read(0x0100, read_buf, 32);
    
    if (memcmp(write_buf, read_buf, 32) == 0)
    {
        printf("✅ 多字节测试通过！\r\n");
    }
    else
    {
        printf("❌ 多字节测试失败！\r\n");
    }
    
    printf("\r\n========================================\r\n");
    printf("  所有测试完成！\r\n");
    printf("========================================\r\n");
}

/**
 * @brief  系统时钟配置
 */
void SystemClock_Config(void)
{
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
    
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
    HAL_RCC_OscConfig(&RCC_OscInitStruct);
    
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
                                | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
    HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
}

int fputc(int ch, FILE *f)
{
    usart_send_byte((uint8_t)ch);
    return ch;
}
